<!doctype html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8" />
    <title>demo</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="./js/jquery-1.12.4.min.js"></script>
</head>
<body>
    <input type="button" id="swipeIDCard" value="读取身份证" />
    <input type="button" id="getExamData" value="读取SQLite数据库" />
    <input type="button" id="ajaxCrossDomain" value="跨域访问" />
    <div id="result"></div>
    <script>
        $('#swipeIDCard').click(function(){
            window.external.swipeIDCard('{"name":"test"}');
        });
        function swipeIDCard(str){
            $('#result').html('<h1>'+new Date().format("yyyy-MM-dd hh:mm:ss")+'</h1>');
            var json = $.parseJSON(str);
            for(var i in json){
                $('#result').append('<h1>【'+i+'】：'+json[i]+'</h1>');
            }
            return str;
        };
        
        $('#getExamData').click(function(){
            window.external.getExamData('SELECT * FROM ylyq_peis_exam_elder');
        });
        function getExamData(str){
            $('#result').html('<h1>'+new Date().format("yyyy-MM-dd hh:mm:ss")+'</h1>');
            var json = $.parseJSON(str);
            for(var i in json){
                $('#result').append('<h1>'+json[i].id+'，'+json[i].exam_no+'，'+json[i].real_name+'，'+json[i].sex+'，'+json[i].age+'</h1>');
            }
            return 1;
        };
        
        $('#ajaxCrossDomain').click(function(){
            $('#result').html('<h1>异步请求中，，，请耐心等待</h1>');
            window.external.ajaxGet('https://xuqingkai.gitee.io/html/json/list.json');
        });
        function ajaxGet(str){
            $('#result').html('<h1>'+new Date().format("yyyy-MM-dd hh:mm:ss")+'</h1>');
            if(str.indexOf('HttpGetError:')==0){
                $('#result').append(str.substr(13));
            }else{
                var json = $.parseJSON(str);
                for(var i in json){
                    $('#result').append('<h1>【'+i+'】：'+json[i]+'</h1>');
                }
                for(var i in json.data){
                    for(var j in json.data[i]){
                        $('#result').append('<h1>【'+j+'】：'+json.data[i][j]+'</h1>');
                    }
                }
            }
            return 1;
        }
        Date.prototype.format = function(format) {
            var o = {
                "M+": this.getMonth() + 1, //month  
                "d+": this.getDate(), //day  
                "h+": this.getHours(), //hour  
                "m+": this.getMinutes(), //minute  
                "s+": this.getSeconds(), //second  
                "q+": Math.floor((this.getMonth() + 3) / 3), //quarter  
                "S": this.getMilliseconds() //millisecond  
            }
            if (/(y+)/.test(format)){
                format = format.replace(RegExp.$1,(this.getFullYear() + "").substr(4 - RegExp.$1.length));
            }
            for (var k in o){
                if (new RegExp("(" + k + ")").test(format)){
                    format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
                }      
            }
                
            return format;
        }
        
    </script>
</body>
</html>

